package com.heima.wemedia.controller.v1;

import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.dtos.NewsAuthDto;
import com.heima.model.wemedia.dtos.WmNewsDto;
import com.heima.model.wemedia.dtos.WmNewsPageReqDto;
import com.heima.model.wemedia.vos.WmNewsVO;
import com.heima.wemedia.service.WmNewsService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.InvocationTargetException;

/**
 * 自媒体文章管理
 */
@RestController
@Slf4j
@RequestMapping("/api/v1/news")
public class WmNewsController {

    @Autowired
    private WmNewsService wmNewsService;

    /**
     * 数据回显
     */
    @GetMapping("/one/{articleId}")
    public ResponseResult oneArticle(@PathVariable Integer articleId){
        log.info("【文章内容回显】");
        return wmNewsService.reCoverArticle(articleId);
    }

    /**
     * 新增接口：根据ID获取文章详情（VO对象）
     */
    @GetMapping("/one_vo/{articleId}")
    public ResponseResult<WmNewsVO> oneVoArticle(@PathVariable Integer articleId) {
        log.info("【根据ID获取文章详情（VO对象）】");
        return wmNewsService.oneVoArticle(articleId);
    }

    /**
     * 自媒体提交文章
     * @param wmNewsDto
     * @return
     */
    @PostMapping("/submit")
    @GlobalTransactional //设置分布式事物入口
    public ResponseResult submitNews(@RequestBody WmNewsDto wmNewsDto) {
        log.info("【提交文章】");
        return wmNewsService.submitNews(wmNewsDto);
    }


    /**
     * 条件查询文章列表
     */
    @PostMapping("/list")
    public ResponseResult list(@RequestBody WmNewsPageReqDto dto){
        log.info("【条件查询自媒体文章列表】");
        return wmNewsService.listNews(dto);
    }

    /**
     * 上架或下架自媒体文章
     * @param dto
     * @return
     */
    @PostMapping("/down_or_up")
    public ResponseResult downOrUp(@RequestBody WmNewsDto dto){
        log.info("【自媒体文章的上下架】");
        return wmNewsService.downOrUp(dto);
    }

    /**
     * 查询文章列表
     */
    @PostMapping("/list_vo")
    public ResponseResult list_vo(@RequestBody NewsAuthDto newsAuthDto){
        log.info("【管理端查询文章列表】");
        return wmNewsService.list_vo(newsAuthDto);
    }

    /**
     * 人工审核成功
     */
    @PostMapping("/auth_pass")
    public ResponseResult authPass(@RequestBody NewsAuthDto newsAuthDto) {
        log.info("【人工审核成功】");
        return wmNewsService.authPass(newsAuthDto);
    }

    /**
     * 人工审核失败
     */
    @PostMapping("/auth_fail")
    public ResponseResult authFail(@RequestBody NewsAuthDto newsAuthDto) {
        log.info("【人工审核失败】");
        return wmNewsService.authFail(newsAuthDto);
    }

}
